<html>
<head><meta charset="utf-8"><title>impl&lt;T&gt; for T or for &amp;T deduction · t-compiler/help · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/index.html">t-compiler/help</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/impl.3CT.3E.20for.20T.20or.20for.20.26T.20deduction.html">impl&lt;T&gt; for T or for &amp;T deduction</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="198601688"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/impl%3CT%3E%20for%20T%20or%20for%20%26T%20deduction/near/198601688" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Derek <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/impl.3CT.3E.20for.20T.20or.20for.20.26T.20deduction.html#198601688">(May 24 2020 at 21:45)</a>:</h4>
<p>I got confused when I met impl &lt;T&gt; for T  and  and impy&lt;T&gt; for &amp;T, but it widely used. e.g. in src/libcore/borrow.rs, Here is a simplified example,<br>
playground link!<br>
<a href="https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=3be71dcf0dec08f85e18fbe3de86f6cb">https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=3be71dcf0dec08f85e18fbe3de86f6cb</a></p>
<div class="codehilite"><pre><span></span><code><span class="c1">// use std::borrow::Borrow;</span>
<span class="k">pub</span><span class="w"> </span><span class="k">trait</span><span class="w"> </span><span class="n">MyBorrow</span><span class="o">&lt;</span><span class="n">Borrowed</span>: <span class="o">?</span><span class="nb">Sized</span><span class="o">&gt;</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">fn</span> <span class="nf">myborrow</span><span class="p">(</span><span class="o">&amp;</span><span class="bp">self</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="kp">&amp;</span><span class="nc">Borrowed</span><span class="p">;</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
<span class="k">impl</span><span class="o">&lt;</span><span class="n">T</span>: <span class="o">?</span><span class="nb">Sized</span><span class="o">&gt;</span><span class="w"> </span><span class="n">MyBorrow</span><span class="o">&lt;</span><span class="n">T</span><span class="o">&gt;</span><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="n">T</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">fn</span> <span class="nf">myborrow</span><span class="p">(</span><span class="o">&amp;</span><span class="bp">self</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="kp">&amp;</span><span class="nc">T</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="n">println</span><span class="o">!</span><span class="p">(</span><span class="s">&quot;T&quot;</span><span class="p">);</span><span class="w"></span>
<span class="w">        </span><span class="bp">self</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
<span class="k">impl</span><span class="o">&lt;</span><span class="n">T</span>: <span class="o">?</span><span class="nb">Sized</span><span class="o">&gt;</span><span class="w"> </span><span class="n">MyBorrow</span><span class="o">&lt;</span><span class="n">T</span><span class="o">&gt;</span><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="o">&amp;</span><span class="n">T</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">fn</span> <span class="nf">myborrow</span><span class="p">(</span><span class="o">&amp;</span><span class="bp">self</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="kp">&amp;</span><span class="nc">T</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="n">println</span><span class="o">!</span><span class="p">(</span><span class="s">&quot;ref T&quot;</span><span class="p">);</span><span class="w"> </span><span class="o">&amp;**</span><span class="bp">self</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
<span class="k">fn</span> <span class="nf">main</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">a</span>:<span class="kt">i32</span> <span class="o">=</span><span class="w"> </span><span class="mi">32</span><span class="p">;</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">aa</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="o">&amp;</span><span class="n">a</span><span class="p">;</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">b</span>:<span class="kp">&amp;</span><span class="kt">i32</span> <span class="o">=</span><span class="w"> </span><span class="n">aa</span><span class="p">.</span><span class="n">myborrow</span><span class="p">();</span><span class="w"></span>
<span class="w">    </span><span class="n">println</span><span class="o">!</span><span class="p">(</span><span class="s">&quot;b = {:?}&quot;</span><span class="p">,</span><span class="w"> </span><span class="n">b</span><span class="p">);</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">aaa</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="o">&amp;</span><span class="n">aa</span><span class="p">;</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">b</span>:<span class="kp">&amp;</span><span class="kt">i32</span> <span class="o">=</span><span class="w"> </span><span class="n">aaa</span><span class="p">.</span><span class="n">myborrow</span><span class="p">();</span><span class="w"></span>
<span class="w">    </span><span class="n">println</span><span class="o">!</span><span class="p">(</span><span class="s">&quot;b = {:?}&quot;</span><span class="p">,</span><span class="w"> </span><span class="n">b</span><span class="p">);</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>


<p>The output is:</p>
<div class="codehilite"><pre><span></span><code>T
b = 32
ref T
b = 32
</code></pre></div>


<p>But aa is also a reference  to i32 why it is not use &amp;T version, and aaa is ref to ref i32. it use &amp;T version. that is the deduction rule applied here? Thanks!</p>



<a name="198601895"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/impl%3CT%3E%20for%20T%20or%20for%20%26T%20deduction/near/198601895" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/impl.3CT.3E.20for.20T.20or.20for.20.26T.20deduction.html#198601895">(May 24 2020 at 21:50)</a>:</h4>
<p><code>myborrow</code> takes <code>&amp;self</code>.</p>
<p><code>aa</code> has the type <code>&amp;i32</code>. This means that <code>impl .. for T</code> does not require any auto refs or derefs..<br>
<code>aaa</code> has the type <code>&amp;&amp;i32</code>. <code>impl ... for T</code> does not directly match in this case, as we would first have automatically apply 1 deref (<code>*aa</code>).<br>
For</p>
<div class="codehilite"><pre><span></span><code><span class="k">impl</span><span class="o">&lt;</span><span class="n">T</span>: <span class="o">?</span><span class="nb">Sized</span><span class="o">&gt;</span><span class="w"> </span><span class="n">MyBorrow</span><span class="o">&lt;</span><span class="n">T</span><span class="o">&gt;</span><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="o">&amp;</span><span class="n">T</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">fn</span> <span class="nf">myborrow</span><span class="p">(</span><span class="o">&amp;</span><span class="bp">self</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="kp">&amp;</span><span class="nc">T</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="n">println</span><span class="o">!</span><span class="p">(</span><span class="s">&quot;ref T&quot;</span><span class="p">);</span><span class="w"> </span><span class="o">&amp;**</span><span class="bp">self</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>


<p><code>myborrow</code> actually expected a <code>&amp;&amp;T</code> as an argument, so we have an exact match here</p>



<a name="198601916"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/impl%3CT%3E%20for%20T%20or%20for%20%26T%20deduction/near/198601916" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/impl.3CT.3E.20for.20T.20or.20for.20.26T.20deduction.html#198601916">(May 24 2020 at 21:51)</a>:</h4>
<p><a href="https://stackoverflow.com/questions/28519997/what-are-rusts-exact-auto-dereferencing-rules">https://stackoverflow.com/questions/28519997/what-are-rusts-exact-auto-dereferencing-rules</a> may also be helpful here</p>



<a name="198706162"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/impl%3CT%3E%20for%20T%20or%20for%20%26T%20deduction/near/198706162" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Derek <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/impl.3CT.3E.20for.20T.20or.20for.20.26T.20deduction.html#198706162">(May 25 2020 at 22:31)</a>:</h4>
<p>Thanks, <span class="user-mention" data-user-id="216206">@lcnr</span> <br>
 because of auto dereferencing, even without impl&lt;T: ?Sized&gt; MyBorrow&lt;T&gt; for &amp;T.   my example here works just fine.  and i got: </p>
<div class="codehilite"><pre><span></span><code>T
b = 32
T
b = 32
</code></pre></div>


<p>Why we need &amp;T version here, in what situation we need a &amp;T version, in what situation we don't need. Thanks.</p>



<a name="198730876"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/impl%3CT%3E%20for%20T%20or%20for%20%26T%20deduction/near/198730876" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/impl.3CT.3E.20for.20T.20or.20for.20.26T.20deduction.html#198730876">(May 26 2020 at 08:00)</a>:</h4>
<p>Most of the times you don't need to implement a trait both for <code>T</code> and <code>&amp;(mut) T</code> but there are some cases where it's useful.<br>
For example <code>std::iter::Iterator</code> is implemented for all <code>&amp;mut I where I: Iterator</code>.</p>
<div class="codehilite"><pre><span></span><code><span class="k">fn</span> <span class="nf">main</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">first</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">5</span><span class="o">..</span><span class="mi">10</span><span class="p">;</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="k">mut</span><span class="w"> </span><span class="n">second</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="o">..</span><span class="mi">10</span><span class="p">;</span><span class="w"></span>

<span class="w">    </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="n">fst</span><span class="p">,</span><span class="w"> </span><span class="n">snd</span><span class="p">)</span><span class="w"> </span><span class="k">in</span><span class="w"> </span><span class="n">first</span><span class="p">.</span><span class="n">zip</span><span class="p">(</span><span class="o">&amp;</span><span class="k">mut</span><span class="w"> </span><span class="n">second</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="c1">// We&#39;re using `&amp;mut second` here, because we want to keep using it afterwards.</span>
<span class="w">        </span><span class="n">println</span><span class="o">!</span><span class="p">(</span><span class="s">&quot;fst: {}, snd: {}&quot;</span><span class="p">,</span><span class="w"> </span><span class="n">fst</span><span class="p">,</span><span class="w"> </span><span class="n">snd</span><span class="p">);</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>

<span class="w">    </span><span class="k">for</span><span class="w"> </span><span class="n">rest</span><span class="w"> </span><span class="k">in</span><span class="w"> </span><span class="n">second</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="n">println</span><span class="o">!</span><span class="p">(</span><span class="s">&quot;        snd: {}&quot;</span><span class="p">,</span><span class="w"> </span><span class="n">rest</span><span class="p">);</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>


<p>For a more detailed answer, stackoverflow and <a href="https://users.rust-lang.org/">https://users.rust-lang.org/</a> can sometimes be more be helpful.</p>



<a name="198849192"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/impl%3CT%3E%20for%20T%20or%20for%20%26T%20deduction/near/198849192" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> yunhua <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/impl.3CT.3E.20for.20T.20or.20for.20.26T.20deduction.html#198849192">(May 27 2020 at 04:42)</a>:</h4>
<p>Thanks!</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>